<?php
	/*
		PandaForum: PHP Forum Engine
		
		Jesse Gill, 0108377707
	*/
	
	require_once('./src/classes/DBConnection.php');
	require('./src/config.php');
	
	header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
	header("Cache-Control: no-store, no-cache, must-revalidate");
	header("Cache-Control: post-check=0, pre-check=0", false);
	header("Pragma: no-cache");
	
	$connection = new DBConnection();
	$connection->connect($host, $username, $password, $dbname);
	
	if (! $connection->is_connected())
	{
		die($connection->get_error());
	}
	
	$username = 					$connection->clean_var($_GET['username']);
	$password =						$connection->clean_var($_GET['password']);
	$retypepassword = 				$connection->clean_var($_GET['retypepassword']);
	$email = 						$connection->clean_var($_GET['email']);
	$day =							$connection->clean_var(intval($_GET['day']));
	$month =						$connection->clean_var(intval($_GET['month']));
	$year =							$connection->clean_var(intval($_GET['year']));
	
	$validate = true;
	$failedreason = "";
	
	// FORM VALIDATION****************************************************************************************************************
	if (strlen($username) < 3 || strlen($username) > 20 || preg_match("[\W]", $username) > 0)
	{
		$failedreason .= "Invalid username.<br />";
		$validate = false;
	}
	
	if (strlen($password) < 5 || strlen($password) > 20 || preg_match("[\W]", $password) > 0 || $password != $retypepassword)
	{
		$failedreason .= "Invalid password or password does not match confirm password.<br />";
		$validate = false;
	}
	
	if (strlen($email) < 5 || preg_match("/.+@.+\..+/", $email) == 0)
	{
		$failedreason .= "Invalid email address.<br />";
		$validate = false;
	}
	
	if (! validateDate($day, $month, $year))
	{
		$failedreason .= "Invalid birthday.<br />";
		$validate = false;
	}
	//***********************************************************************************************************************************
	
	if (! $validate)
	{
		die("<div id='register_failed_message'>$failedreason</div>");
	}
	
	$row = $connection->get_result_array("SELECT id FROM user WHERE username='$username'");
	
	if (!empty($row))
	{
		die("<div id='register_failed_message'>A user with the name $username already exists, try another username.</div>");
	}
	
	$row = $connection->get_result_array("SELECT id FROM user WHERE email='$email'");
	
	if (!empty($row))
	{
		die("<div id='register_failed_message'>$email has already been registered to a user, try another email address.</div>");
	}
	
	$query = "INSERT INTO user SET 
				username='$username',
				password=SHA('$password'),
				email='$email',
				birthdate='$year-$month-$day 00:00:00',
				joindate=NOW(),
				lastlogin=NOW(),
				lastactive=NOW(),
				sid='-1'
			";
	
	if ($connection->query_noresult($query) == 0)
	{
		die($connection->get_error());
	}
	
	$row = $connection->get_result_array("SELECT id FROM user WHERE username='$username'");
	
	if (empty($row))
	{
		die($connection->get_error());
	}
	
	if ($connection->query_noresult("INSERT INTO user_preferences SET userid='" . $row['id'] . "'") == 0)
	{
		die($connection->get_error());
	}
	
	if ($connection->query_noresult("INSERT INTO user_signature SET userid='" . $row['id'] . "'") == 0)
	{
		die($connection->get_error());
	}
	
	if ($connection->query_noresult("INSERT INTO user_ranks SET userid='" . $row['id'] . "', rankid='2', displayedrank=1") == 0)
	{
		die($connection->get_error());
	}
	
	echo "<div id='register_success_message'>Registration successful!</div>";
	
	$connection->close();
	
	// returns true if the given date is valid
	function validateDate($d, $m, $y) // day, month, year
	{
		$months = array(1 => 31, 2 => 28, 3 => 31, 4 => 30, 5 => 31, 6 => 30, 7 => 31, 8 => 31, 9 => 30, 10 => 31, 11 => 30, 12 => 31);
		
		if ($d < 1 || $d > 31) { return false; }
		if ($m < 1 || $m > 12) { return false; }
		if ($y < 1 || $y > intval(date("Y"))) { return false; }
		
		// Is the month february?
		if ($m == 2)
		{
			// Check for a leap year
			if ($y % 4 == 0)
			{
				if ($d > 29) { return false; }
			}
			else
			{
				if ($d > 28) { return false; }
			}
		}
		else
		{
			if ($d > $months[$m]) { return false; }
		}
		
		return true;
	}
?>